home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / userflop.2 / userflop / userfloppy0.2 / userfdformat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-28  |  1.3 KB  |  72 lines

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <linux/fs.h>
  8. #include <linux/major.h>
  9.  
  10. #define FDFORMAT "/usr/bin/fdformat"
  11.  
  12. /* Este programa permite llamar a fdformat a un usuario normal 
  13.    controlando que no haga trampas... (puede formatear solo los 
  14.    /dev/fd[0-1]* y solo si no son links. */
  15.  
  16. int main(int argc, char* argv[]) {
  17.  
  18.   uid_t realuser;
  19.   int fd;
  20.   char* floppy;
  21.   struct stat floppy_stat;
  22.   realuser=getuid();
  23.   floppy=argv[1];
  24.  
  25.   if (geteuid()) {
  26.     fprintf(stderr,"%s: I must be installed setuid root!\n",argv[0]);
  27.     exit(1);
  28.   }
  29.  
  30.   if ( strncmp(floppy,"/dev/fd0",8) && strncmp(floppy,"/dev/fd1",8) ) {
  31.     fprintf(stderr,"Normal user can only format /dev/fd[01]* devices\n");
  32.     exit(1);
  33.   }
  34.  
  35.   /* Can I open file? Change my uid to realuser and try */
  36.  
  37.   setreuid(0,realuser); 
  38.   fd=open(floppy, O_RDWR);
  39.   if (fd==-1) {
  40.     perror(floppy);
  41.     exit(1);
  42.   }
  43.  
  44.   /* Ok, back to superuser */
  45.  
  46.   setreuid(realuser,0);
  47.  
  48.   if (fstat(fd,&floppy_stat)==-1 ) {
  49.     perror(floppy);
  50.     exit(1);
  51.   }
  52.  
  53.   if (MAJOR(floppy_stat.st_rdev)!=FLOPPY_MAJOR) {
  54.     fprintf(stderr,"Huh? %s is not a floppy! \n",floppy);
  55.     exit(1);
  56.   }
  57.  
  58.   if (close(fd)==-1) {
  59.     perror("Close??? ");
  60.     exit(1);
  61.   }
  62.  
  63.   execv(FDFORMAT,argv);
  64.  
  65.   perror("exec :");
  66.   exit(1);
  67.   
  68. }
  69.  
  70.  
  71.   
  72.